package com.ymatou.iapi.coupon.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.ActivityPriceDto;
import com.ymatou.iapi.coupon.parameter.GetProductAvailableCouponListBean;
import com.ymatou.iapi.coupon.parameter.GetProductBestCouponBean;
import com.ymatou.iapi.coupon.parameter.RuleDto;
import com.ymatou.iapi.coupon.parameter.getFinalCouponDataBean;
import com.ymatou.iapi.coupon.service.getFinalCouponDataCall;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceCallServiceV2;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.utils.TestDataManager;
/**
*商详最优和可用的买手券和平台券合并
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getFinalCouponData{
	private static getFinalCouponDataBean bean;
	private static getFinalCouponDataCall call;
	
	
	
	private  SellerCreateBatchBean sellerCreateBatchBean=new SellerCreateBatchBean();
	private  SellerCreateBatchCall sellerCreateBatchCall=new SellerCreateBatchCall();
	
	
	private  CreateCouponBean createCouponBean;
	private  CreateCouponCall createCouponCall;
	private  couponWapper couponWapper=new couponWapper();
	private couponbatchWapper couponbatchWapper=new couponbatchWapper();
	
	private CouponServiceMCallServiceV2 couponMService=new CouponServiceMCallServiceV2();
	private CouponServiceCallServiceV2 couponService=new CouponServiceCallServiceV2();
	//平台券没有指定任何条件
	private final String ruleId_All="e0d889ac817a4c6ea9188baa4d909d11";
	private final Integer operatorId=222222; //操作人
	private final String batchCode="NUVGKT1CKX";
	//平台券指定商品
	private final String ruleId_productId="a740cf9fe22a46dc91c89c09c72e61cf";
	private final String batchCode_productId="9PWHYKALTN";
	private final String productId="productId999";
	//平台券指定类目
	private final String batchCode_categories="YJMGNSAD12";
	private final Integer categoriesId=1001;
	private final String ruleId_categoriesId="150e3e4dd51d4b73852878b3219df31f";
	//平台券指定品牌
	private final String batchCode_brandId="UW4G6CL5K4";
	private final Integer brandId=500;
	private final String ruleId_brandId="66c69f29afba4b9bae8e1160c7828ba9";
	//平台券指定国家
	private final String batchCode_countryId="2Z6QLEQ5Q3";
	private final Integer countryId=10;
	private final String ruleId_countryId="a3e59ef735f64278a0c65093b6cf0503";
	//平台券指定活动
	private final String batchCode_activityId="4Z819QC1N9";
	private final Integer activityId=11;
	private final String ruleId_activityId="0fd76c0ee8f9485eab75a2ec31f9327c";
	//平台券指定psp
	private final String batchCode_isPsp="U5QFRZSAFF";
	private final boolean isPsp=true;
	private final String ruleId_isPsp="9873bbeb86ec4c18b5fdbe56af7023ce";
	//平台券指定使用团长券
	private final String batchCode_isGroup="CVHZTRMVEV";
	private final boolean isGroup=true;
	private final String ruleId_isGroup="ff31ec7e321a42a98c464ce397a24433";
	//平台券指定platformSource=0 c2c
	private final String batchCode_platformSource0="GHY5S1WPA2";
	private final Integer platformSource0=0;
	private final String ruleId_platformSource0="b234a0e9b93644a6abd4578fe639b100";
	//平台券指定platformSource=1 全球优选
	private final String batchCode_platformSource1="LN7HLL8JJA";
	private final Integer platformSource1=1;
	private final String ruleId_platformSource1="920e28b0e3444582891bbedff0972a93";
	//平台券设置每人可以领取2次
	private final String batchCode_receiveTimes2="V4XEHNTEWA";
	private final String ruleId_receiveTimes2="e3601aa38d71434b84cd1fa2ef97e1c1";
	
	//规则中有两张券
	private final String ruleId_two_4000000_3800000="cf398958b8d34f3a8d6a493cd4baea69";
	private final String ruleId_two_4000000_3900000="b8a41cf48808412bb6f9e28d0ee85380";
	private final String ruleId_two_4000000_3700000="e95a71ee1d5d42e78d3c0c4635cc8840";
	private final String batchCode_two_4000000_3800000="3V294LCJKZ";//满4000000减3800000
	private final String batchCode_two_4000000_3900000="ELMX48LFHZ";//满4000000减3900000
	private final String batchCode_two_4000000_3700000="TFXLSYWE58";//满4000000减3700000
	
	//新人优惠券
	private final String batchCode_newUser1="76ENJ46G8R";
	private final String batchCode_newUser2="WL374CVCMR";
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商详最优和可用的买手券和平台券合并");
	}

	@Before
	public void caseUp() {
		bean=new getFinalCouponDataBean();
		call=new getFinalCouponDataCall();
		createCouponBean=new CreateCouponBean();
		createCouponCall=new CreateCouponCall();
		couponbatchWapper.setNoInvalid(this.batchCode);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	public void receiveCouponInfoByRule(Integer userId,String ruleId) throws ClientProtocolException, IOException
	{
		String deviceId="aaa"+TestDataManager.getRandomNum(5);
		String url="http://couponmarketingbiz.iapi.ymatou.com/api/couponmkting/ReceiveActivityCoupon";
		String str="{\"userId\":"+userId+",\"deviceId\":\""+deviceId+"\",\"activityname\":\""+ruleId+"\"}";
		YmatouEasyCall ec=new YmatouEasyCall(url, "POST", "JSON");
		ec.setData(str);
		ec.callService();
		String data=ec.getReturnData();
		System.out.print(data);
	}
	/**
	 * 设置默认的创建商家优惠券券,仅用于本接口 满3000000减3000
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public SellerCreateBatchBean DefaultSellerCreateBatchCall() throws ClientProtocolException, IOException {
		//SellerCreateBatchBean sellerCreateBatchBean = new SellerCreateBatchBean();

		int sellerid = TestDataManager.getRandomIntNum(6);
		sellerCreateBatchBean.setSellerId(sellerid);
		sellerCreateBatchBean.setBatchName("java化商家优惠券批次Name");
		sellerCreateBatchBean.setBatchDes("创建商家优惠券批次描述");

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 1); // 
		dAfter = calendar2.getTime(); // 得到前一天的时间

		sellerCreateBatchBean.setEffectiveStartDate(dBefore);
		sellerCreateBatchBean.setEffectiveEndDate(dAfter);

		sellerCreateBatchBean.setOrderMinAmount(new BigDecimal(3000000));
		sellerCreateBatchBean.setCouponAmount(new BigDecimal(3000));

		sellerCreateBatchBean.setCouponTotalCount(1000);
		sellerCreateBatchBean.setReceiveNumPerUser(1);
		sellerCreateBatchBean.setIsShowOnPage(true);
		sellerCreateBatchBean.setCouponType((byte) 1);

		couponMService.deleteSellerBatchCountInCurrentMonthBysellerIdAnd30(sellerCreateBatchBean);
		//SellerCreateBatchCall sellerCreateBatchCall = new SellerCreateBatchCall();
		sellerCreateBatchCall.setData(sellerCreateBatchBean);
		sellerCreateBatchCall.callService();
		
		return sellerCreateBatchBean;
	}
	
	
	
	/**
	 * 
	 * @param ruleId 规则Id
	 * @return
	 */
	private GetProductAvailableCouponListBean initGetProductAvailableCoupon(Integer operatorId,String ruleId)
	{
		GetProductAvailableCouponListBean bean=new GetProductAvailableCouponListBean();
		Integer buyerId=TestDataManager.getRandomIntNum(5);
		String productId="productId_"+TestDataManager.getRandomIntNum(5);
		bean.setBuyerId(buyerId);
		List<RuleDto> rules=new ArrayList();
		RuleDto ruleDto=new RuleDto();
		ruleDto.setRuleId(ruleId);//领券规则id（新人优惠券无规则ID）
		ruleDto.setMode(0);//0 = 平台优惠券 1=新人优惠券
		//2未下单会员,8已下单会员 32普通 64白银 128黄金 256铂金 512钻石 1024黑卡 2048非安利会员/大人 4096安利会员 8192安利达人 16黄牛
		ruleDto.setUserType(2);//用户身份
		rules.add(ruleDto);
		
		bean.setRules(rules);
		bean.setSellerId(operatorId);
		bean.setProductId(productId);
		
		List<Integer> categoryIds=new ArrayList();
		categoryIds.add(1001);
		categoryIds.add(1002);
		categoryIds.add(1003);
		bean.setCategoryIds(categoryIds);
		
		bean.setBrandId(200);
		//bean.setCountryId(10);
		bean.setPrice(new BigDecimal(70000000));
		
		bean.setMemberGrade(0);
		bean.setPsp(false);
		bean.setPlatformSource(0);//平台来源（0-C2C，1-全球优选)
		bean.setUseCommander(false);
		bean.setPlatformId(0);
		bean.setCouponUseType(0);//在活动上使用限制 0 无限制  1不可用买手券 2不可用平台券 3不可用买手券以及平台券
		
		return bean;
	}
	
	
	
	/**
	 * 
	 * @param ruleId 规则Id
	 * @return
	 */
	private GetProductBestCouponBean initGetProductBestCoupon(Integer operatorId,String ruleId)
	{
		GetProductBestCouponBean bean=new GetProductBestCouponBean();
		Integer buyerId=TestDataManager.getRandomIntNum(5);
		String productId="productId_"+TestDataManager.getRandomIntNum(5);
		bean.setBuyerId(buyerId);
		List<RuleDto> rules=new ArrayList();
		RuleDto ruleDto=new RuleDto();
		ruleDto.setRuleId(ruleId);//领券规则id（新人优惠券无规则ID）
		ruleDto.setMode(0);//0 = 平台优惠券 1=新人优惠券
		//2未下单会员,8已下单会员 32普通 64白银 128黄金 256铂金 512钻石 1024黑卡 2048非安利会员/大人 4096安利会员 8192安利达人 16黄牛
		ruleDto.setUserType(2);//用户身份
		rules.add(ruleDto);
		
		bean.setRules(rules);
		bean.setSellerId(operatorId);
		bean.setProductId(productId);
		
		List<Integer> categoryIds=new ArrayList();
		categoryIds.add(1001);
		categoryIds.add(1002);
		categoryIds.add(1003);
		bean.setCategoryIds(categoryIds);
		
		bean.setBrandId(200);
		//bean.setCountryId(10);
		
		List<ActivityPriceDto> activityPriceList=new ArrayList();
		ActivityPriceDto activityPriceDto=new ActivityPriceDto();
		activityPriceDto.setActivityId(11);//不允许更改
		activityPriceDto.setPrice(new BigDecimal(7000000));
		activityPriceDto.setDisplay(true);
		activityPriceList.add(activityPriceDto);
		bean.setActivityPriceList(activityPriceList);
		
		bean.setMemberGrade(0);
		bean.setPsp(false);
		bean.setPlatformSource(0);//平台来源（0-C2C，1-全球优选)
		bean.setUseCommander(false);
		bean.setPlatformId(0);
		bean.setCouponUseType(0);//在活动上使用限制 0 无限制  1不可用买手券 2不可用平台券 3不可用买手券以及平台券
		
		return bean;
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFinalCouponData_001() {
		Logger.start(true,"商详最优和可用的买手券和平台券合并-未领-返回成功");
		try {

			GetProductAvailableCouponListBean availableReq=new GetProductAvailableCouponListBean();
			GetProductBestCouponBean bestReq=new GetProductBestCouponBean();
			
			//可用列表
			availableReq=initGetProductAvailableCoupon(this.operatorId,this.ruleId_two_4000000_3900000);
			Integer userId=availableReq.getBuyerId();
			Integer sellerId=TestDataManager.getRandomIntNum(10);
			availableReq.setSellerId(sellerId);
			
			//最优
			bestReq=initGetProductBestCoupon(this.operatorId,this.ruleId_two_4000000_3900000);
			bestReq.setBuyerId(userId);
			bestReq.setSellerId(sellerId);
			
			//买手优惠券1
			SellerCreateBatchBean sellerCreateBatchBean1=couponMService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean1.setSellerId(sellerId);
			sellerCreateBatchBean1.setOrderMinAmount(new BigDecimal(4000000));
			sellerCreateBatchBean1.setCouponAmount(new BigDecimal(3600000));
			SellerCreateBatchCall sellerCreateBatchCall1=couponMService.SellerCreateBatch(sellerCreateBatchBean1);
			//买手优惠券2
			SellerCreateBatchBean sellerCreateBatchBean2=couponMService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean2.setSellerId(sellerId);
			sellerCreateBatchBean2.setOrderMinAmount(new BigDecimal(4000000));
			sellerCreateBatchBean2.setCouponAmount(new BigDecimal(3700000));
			SellerCreateBatchCall sellerCreateBatchCall2=couponMService.SellerCreateBatch(sellerCreateBatchBean2);
			
			bean.setGetProductAvailableCouponReq(availableReq);
			bean.setGetProductBestCouponReq(bestReq);
			call.setData(bean);
			call.callService();			
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFinalCouponData_002() {
		Logger.start(true,"商详最优和可用的买手券和平台券合并-已领-已领取1次-返回成功");
		try {

			GetProductAvailableCouponListBean availableReq=new GetProductAvailableCouponListBean();
			GetProductBestCouponBean bestReq=new GetProductBestCouponBean();
			
			//可用列表
			availableReq=initGetProductAvailableCoupon(this.operatorId,this.ruleId_two_4000000_3900000);
			Integer userId=availableReq.getBuyerId();
			Integer sellerId=TestDataManager.getRandomIntNum(10);
			availableReq.setSellerId(sellerId);
			
			//最优
			bestReq=initGetProductBestCoupon(this.operatorId,this.ruleId_two_4000000_3900000);
			bestReq.setBuyerId(userId);
			bestReq.setSellerId(sellerId);
			
			//买手优惠券1
			SellerCreateBatchBean sellerCreateBatchBean1=couponMService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean1.setSellerId(sellerId);
			sellerCreateBatchBean1.setOrderMinAmount(new BigDecimal(4000000));
			sellerCreateBatchBean1.setCouponAmount(new BigDecimal(3600000));
			SellerCreateBatchCall sellerCreateBatchCall1=couponMService.SellerCreateBatch(sellerCreateBatchBean1);
			//买手优惠券2
			SellerCreateBatchBean sellerCreateBatchBean2=couponMService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean2.setSellerId(sellerId);
			sellerCreateBatchBean2.setOrderMinAmount(new BigDecimal(4000000));
			sellerCreateBatchBean2.setCouponAmount(new BigDecimal(3700000));
			SellerCreateBatchCall sellerCreateBatchCall2=couponMService.SellerCreateBatch(sellerCreateBatchBean2);
			
			//领取2买手
			couponMService.receiveCouponInfo(sellerCreateBatchCall1.getBatchCode(), userId);
			couponMService.receiveCouponInfo(sellerCreateBatchCall2.getBatchCode(), userId);
			//领取平台
			this.receiveCouponInfoByRule(userId, this.ruleId_two_4000000_3900000);
			
			bean.setGetProductAvailableCouponReq(availableReq);
			bean.setGetProductBestCouponReq(bestReq);
			call.setData(bean);
			call.callService();			
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFinalCouponData_003() {
		Logger.start(true,"商详最优和可用的买手券和平台券合并-优惠券可以领取2次-已领取2次-返回成功");
		try {

			GetProductAvailableCouponListBean availableReq=new GetProductAvailableCouponListBean();
			GetProductBestCouponBean bestReq=new GetProductBestCouponBean();
			
			//可用列表
			availableReq=initGetProductAvailableCoupon(this.operatorId,this.ruleId_receiveTimes2);
			Integer userId=availableReq.getBuyerId();
			Integer sellerId=TestDataManager.getRandomIntNum(10);
			availableReq.setSellerId(sellerId);
			//最优
			bestReq=initGetProductBestCoupon(this.operatorId,this.ruleId_receiveTimes2);
			bestReq.setBuyerId(userId);
			bestReq.setSellerId(sellerId);
			
			//买手优惠券1
			SellerCreateBatchBean sellerCreateBatchBean1=couponMService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean1.setSellerId(sellerId);
			sellerCreateBatchBean1.setOrderMinAmount(new BigDecimal(4000000));
			sellerCreateBatchBean1.setCouponAmount(new BigDecimal(3600000));
			sellerCreateBatchBean1.setReceiveNumPerUser(2);
			SellerCreateBatchCall sellerCreateBatchCall1=couponMService.SellerCreateBatch(sellerCreateBatchBean1);
			//买手优惠券2
			SellerCreateBatchBean sellerCreateBatchBean2=couponMService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean2.setSellerId(sellerId);
			sellerCreateBatchBean2.setOrderMinAmount(new BigDecimal(4000000));
			sellerCreateBatchBean2.setCouponAmount(new BigDecimal(3700000));
			sellerCreateBatchBean2.setReceiveNumPerUser(2);
			SellerCreateBatchCall sellerCreateBatchCall2=couponMService.SellerCreateBatch(sellerCreateBatchBean2);
			
			//领取2买手
			couponMService.receiveCouponInfo(sellerCreateBatchCall1.getBatchCode(), userId);
			couponMService.receiveCouponInfo(sellerCreateBatchCall1.getBatchCode(), userId);
			couponMService.receiveCouponInfo(sellerCreateBatchCall2.getBatchCode(), userId);
			couponMService.receiveCouponInfo(sellerCreateBatchCall2.getBatchCode(), userId);
			//领取平台
			this.receiveCouponInfoByRule(userId, this.ruleId_receiveTimes2);
			this.receiveCouponInfoByRule(userId, this.ruleId_receiveTimes2);
			
			bean.setGetProductAvailableCouponReq(availableReq);
			bean.setGetProductBestCouponReq(bestReq);
			call.setData(bean);
			call.callService();			
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}